package org.celery.client.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.celery.client.vo.GoodsVo;
import org.celery.modules.goods.entity.Goods;
import org.celery.modules.goods.entity.GoodsPackage;
import org.celery.modules.goods.entity.GoodsStyle;
import org.celery.modules.goods.service.IGoodsPackageService;
import org.celery.modules.goods.service.IGoodsService;
import org.celery.modules.goods.service.IGoodsStyleService;
import org.celery.modules.goods.wrapper.GoodsPackageWrapper;
import org.celery.modules.goods.wrapper.GoodsStyleWrapper;
import org.celery.modules.goods.wrapper.GoodsWrapper;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("goods")
@Api(value = "商品")
public class ClientGoodsController {

    private final IGoodsService goodsService;
    private final IGoodsStyleService goodsStyleService;
    private final IGoodsPackageService goodsPackageService;

    /**
     * 商品详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<GoodsVo> detail(String goodsId) {
        Goods detail = goodsService.getById(goodsId);

        GoodsVo goodsVo = new GoodsVo();
        if (Func.isNotEmpty(detail)) {
            goodsVo.setGoods(GoodsWrapper.build().entityVO(detail));
            List<GoodsStyle> goodsStyleList = goodsStyleService.list(Wrappers.<GoodsStyle>lambdaQuery()
                    .eq(GoodsStyle::getGoodsId, goodsId)
            );
            goodsVo.setGoodsStyles(GoodsStyleWrapper.build().listVO(goodsStyleList));

            List<GoodsPackage> goodsPackageList = goodsPackageService.list(Wrappers.<GoodsPackage>lambdaQuery()
                    .eq(GoodsPackage::getGoodsId, goodsId)
            );
            goodsVo.setGoodsPackages(GoodsPackageWrapper.build().listVO(goodsPackageList));
        }
        return R.data(goodsVo);
    }

}
